/**
 * @swagger
 * /api/room/calculation-analysis:
 *   get:
 *     tags:
 *       - 房屋管理
 *     summary: 获取房屋计算分析数据
 *     description: 分析房屋面积计算、利用率和空间配置
 *     responses:
 *       200:
 *         description: 成功获取计算分析数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                   description: 响应状态码
 *                 message:
 *                   type: string
 *                   example: "计算分析获取成功"
 *                   description: 响应消息
 *                 data:
 *                   type: object
 *                   properties:
 *                     publicRoomRatio:
 *                       type: string
 *                       description: 公用房间占总房间数的比例（%）
 *                     usageAreaRatio:
 *                       type: string
 *                       description: 使用面积和建筑面积的比例（%）
 *                     averageRoomsPerBuilding:
 *                       type: string
 *                       description: 平均每栋楼的房间数
 *                     roomsPerSquareMeter:
 *                       type: string
 *                       description: 每平方米建筑面积对应的房间数
 *                     baseData:
 *                       type: object
 *                       properties:
 *                         totalRooms:
 *                           type: number
 *                           description: 总房间数
 *                         publicRooms:
 *                           type: number
 *                           description: 公用房间数
 *                         buildingCount:
 *                           type: number
 *                           description: 楼栋数
 *                         totalBuildingArea:
 *                           type: number
 *                           description: 总建筑面积
 *                         totalUsageArea:
 *                           type: number
 *                           description: 总使用面积
 *                     calculations:
 *                       type: object
 *                       properties:
 *                         publicRoomRatioDesc:
 *                           type: string
 *                           description: 公用房间比例计算说明
 *                         usageAreaRatioDesc:
 *                           type: string
 *                           description: 使用面积比例计算说明
 *                         averageRoomsDesc:
 *                           type: string
 *                           description: 平均房间数计算说明
 *                         roomDensityDesc:
 *                           type: string
 *                           description: 房间密度计算说明
 *       500:
 *         description: 服务器内部错误
 */
const { query } = require('../../../lib/db')
const { success, error, handleResponse } = require('../../../lib/response')
const { withCors } = require('../../../lib/cors')

/**
 * 处理房屋计算分析请求
 * @param {Object} req - 请求对象
 * @param {Object} res - 响应对象
 */
async function handler(req, res) {

  try {
    const sql = `
      SELECT 
        SUM(ZFJJS) as totalRooms,
        SUM(GYFJS) as publicRooms,
        SUM(LDS) as buildingCount,
        SUM(XXBXZJZMJ) as totalBuildingArea,
        SUM(XXBXZSYYMJ) as totalUsageArea
      FROM wd_zcgk_bxzyhz
    `

    const result = await query(sql)
    
    if (!result || result.length === 0) {
      return handleResponse(res, error('暂无房产数据', 404))
    }

    const data = result[0]
    const totalRooms = parseInt(data.totalRooms) || 0
    const publicRooms = parseInt(data.publicRooms) || 0
    const buildingCount = parseInt(data.buildingCount) || 0
    const totalBuildingArea = parseFloat(data.totalBuildingArea) || 0
    const totalUsageArea = parseFloat(data.totalUsageArea) || 0

    // 计算各种比例和分析指标
    const analysis = {
      // 公用房间占总房间数的比例
      publicRoomRatio: totalRooms > 0 ? ((publicRooms / totalRooms) * 100).toFixed(2) : 0,
      
      // 使用面积和建筑面积的比例
      usageAreaRatio: totalBuildingArea > 0 ? ((totalUsageArea / totalBuildingArea) * 100).toFixed(2) : 0,
      
      // 平均每栋楼有多少房间
      averageRoomsPerBuilding: buildingCount > 0 ? (totalRooms / buildingCount).toFixed(2) : 0,
      
      // 每平方米建筑面积对应多少房间
      roomsPerSquareMeter: totalBuildingArea > 0 ? (totalRooms / totalBuildingArea).toFixed(6) : 0,
      
      // 原始数据
      baseData: {
        totalRooms,
        publicRooms,
        buildingCount,
        totalBuildingArea,
        totalUsageArea
      },
      
      // 计算说明
      calculations: {
        publicRoomRatioDesc: `公用房间${publicRooms}间 / 总房间${totalRooms}间 × 100%`,
        usageAreaRatioDesc: `使用面积${totalUsageArea}㎡ / 建筑面积${totalBuildingArea}㎡ × 100%`,
        averageRoomsDesc: `总房间${totalRooms}间 / 楼栋数${buildingCount}栋`,
        roomDensityDesc: `总房间${totalRooms}间 / 建筑面积${totalBuildingArea}㎡`
      }
    }

    return handleResponse(res, success(analysis, '计算比较分析获取成功'))

  } catch (err) {
    console.error('计算比较接口错误:', err)
    return handleResponse(res, error('服务器内部错误', 500))
  }
}

export default withCors(handler, ['GET'])